package com.gitee.ykr.lightweight.rpc.protocol.client;

import com.gitee.ykr.lightweight.rpc.common.entity.NettyRpcResponse;
import com.gitee.ykr.lightweight.rpc.common.entity.ResponseSyncEntity;

/**
 * @author YKR
 * @Describe
 * @date 2021/8/24
 */
public class ResponseHandler extends AbstractResponseHandler{


    @Override
    public void handle(NettyRpcResponse response) {
        response.setEndTime(System.currentTimeMillis());
        System.out.println(String.format("Client received: " + response.toString()));
        System.out.println(String.format("耗时: " + (response.getEndTime() - response.getStartTime()) + "ms"));
        //处理response
        if (response.isSyn()) {
            String messageId = response.getMessageId();
            ResponseSyncEntity entity = cacheContainer.getSyncEntityMap().get(messageId);
            if (entity != null) {
                try {
                    entity.setResult(response);
                    entity.getBarrier().await();
                }catch (Exception e){
                    e.printStackTrace();
                    //TODO 异常
                }finally {
                    cacheContainer.getSyncEntityMap().remove( messageId );
                }
            }

        }

    }
}
